<?php

// Namespace

namespace Loop;


/**
 * CLocationHelper helps developers manipulate object geolocation
 *
 * @package		Loop
 * @category	Helper
 */

class CLocationHelper
{

	/**
	 *	Generates a random location from given source latitude(48.802482°), source longitude(2.135100°) and distance (km)
	 *
	 * @param	float	$sourceLatitude		The source latitude
	 * @param	float	$sourceLongitude 	The source longitude
	 * @param	int		$maxDistance		The maximum distance of the region
	 *
	 * @return	An array which contains the location values (latitude and longitude)
	 */

	public function generateRandomLocation ($sourceLatitude, $sourceLongitude, $maxDistance)
	{
		if
		(
			empty($sourceLatitude) ||
			empty($sourceLongitude) ||
			empty($maxDistance)
		)
		{
			return;
		}


		$location =  array();


		// Convert source latitude and source longitude into radians

		$sourceLatitudeRadians = deg2rad($sourceLatitude);
		$sourceLongitudeRadians = deg2rad($sourceLongitude);
		

		// Convert the maximum distance into radians

		// Set the earth radius (in km)

		$earthRadius = (float)6372.796924;

		$pi = pi();

		$randomValueForDistance = $this->getRandomFloatNumber(0, 1.0);
		$randomValueForOrientation = $this->getRandomFloatNumber(0, 1.0);
		
		
		// Convert the maximum distance into radians

		$maxDistanceRadians = $maxDistance/$earthRadius;


		// Compute a random distance

		$randomDistanceRadians = acos
								(
									$randomValueForDistance *(cos($maxDistanceRadians) - 1) + 1
								);


		// Compute a random orientation

		$randomOrientationRadians = (2 * $pi) * $randomValueForOrientation;
	

		// Compute the new random latitude and the new random longitude

		$randomLatitude = asin
						(
							sin($sourceLatitudeRadians) * cos($randomDistanceRadians) + 
							cos($sourceLatitudeRadians)*sin($randomDistanceRadians)*cos($randomOrientationRadians)
						);

		$tempLongitude = $sourceLongitudeRadians + atan2
						(
							sin($randomOrientationRadians)*sin($randomDistanceRadians)*cos($sourceLatitudeRadians), 
							cos($randomDistanceRadians)-sin($sourceLatitudeRadians)*sin($randomLatitude)
						 );

		
		if ($tempLongitude < -$pi)
		{
			$randomLongitude = $tempLongitude + (2 * $pi);
		}
		else if ($tempLongitude > $pi)
		{
			$randomLongitude = $tempLongitude - (2  * $pi);
		}
		else
		{
			$randomLongitude = $tempLongitude;
		}


		// Convert result latitude and result longitude into degrees and store them into an array

		$location['latitude'] = rad2deg($randomLatitude);
		$location['longitude'] = rad2deg($randomLongitude);


		return $location;
	}


	/**
	 *	Generates a random float number for a given range
	 *
	 * @param	int	$min The minimum of the range
	 * @param	int	$max The maximum of the range
	 *
	 * @return	The random float number
	 */

	public function getRandomFloatNumber($min, $max)
	{
		
		$range = $max-$min;
		$num = $min + $range * mt_rand(0, 32767)/32767;


		// Set the number of decimals	

		$randomFloatNumber = round($num, 6);

		
		return ((float) $randomFloatNumber);
	}
}

?>
